package com.cn.wanxi.servlet.back.news;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.INewsTypeService;
import com.cn.wanxi.service.impl.NewsTypeServiceImpl;
import com.cn.wanxi.util.DataEncoding;
import com.cn.wanxi.util.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/back/newstype/info")
public class NewsTypeInfoServlet extends HttpServlet {
    INewsTypeService newsTypeService = new NewsTypeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //新闻类型详情
        String nid = req.getParameter("id");
        int id = 0;
        if (!"".equals(nid))
            id = Integer.parseInt(nid);
        NewsTypeModel newsTypeModel = newsTypeService.getNewsType(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("newstype",newsTypeModel);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //新闻类型分页列表及模糊查询
        String name = req.getParameter("name");
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        String sort = req.getParameter("sort");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("page",(page-1)*size);
        map.put("size",size);
        map.put("sort",sort);
        PageModel<NewsTypeModel> navList = newsTypeService.getNewsTypes(map);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("newstype",navList);
        resp.getWriter().print(jsonObject);
    }
}
